//
//  TranslationAsGroupAction.cpp
//  fingeradvanture
//
//  Created by shuaibincheng on 16/5/21.
//
//

#include "TranslationAsGroupAction.hpp"
#include "TroopAction.hpp"
#include "TroopForm.hpp"

void TranslationAsGroupAction::train(const Vector<cocos2d::Node *> &monsters)
{
    //计算向左侧移动的距离
    auto leftTranslation = form->marginLeft();
    //计算向右侧移动的距离
    auto rightTranslation = form->marginRight();
    
    for(auto it = monsters.begin(); it != monsters.end(); it++)
    {
        auto leftMoveBy = MoveBy::create(1.0f, Vec2(-leftTranslation, 0.0f));
        auto rightMoveBy = MoveBy::create(1.0f, Vec2(rightTranslation, 0.0f));
        
        auto sequence = Sequence::create(leftMoveBy, leftMoveBy->reverse(), rightMoveBy, rightMoveBy->reverse(), NULL);
        auto repeat = Repeat::create(sequence, 10);
        
        auto downMoveBy = MoveBy::create(15.0f, Vec2(0.0f, -form->getVisibleSize().height
                                                                - form->getTroopSize().height - 20));
        
        auto spawn = Spawn::create(repeat,downMoveBy, NULL);
        (*it)->runAction(spawn);
    }
}